package com.cenluan.common;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.cenluan.IndexController;
import com.cenluan.beetl.BeetlRenderFactory;
import com.cenluan.beetl.XSSDefenseFormat;
import com.cenluan.gallery.Gallery;
import com.cenluan.gallery.GalleryController;
import com.cenluan.login.LoginController;
import com.cenluan.login.LoginInterceptor;
import com.cenluan.mi.MiController;
import com.cenluan.photo.Photo;
import com.cenluan.photo.PhotoController;
import com.cenluan.qq.QQController;
import com.cenluan.session.SessionInViewInterceptor;
import com.cenluan.user.User;
import com.cenluan.user.UserController;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;

public class Config extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("env.properties");
		// 开发模式
		if (getPropertyToBoolean("devMode")) {
			me.setDevMode(true);
		}
		// 错误页面使用jfinal默认的，不再设置
		// beetl
		me.setMainRenderFactory(new BeetlRenderFactory());
		// beetl设置全局变量
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("ctx", JFinal.me().getContextPath());
		BeetlRenderFactory.GT.setSharedVars(model);
		BeetlRenderFactory.GT.registerFormat("xss", new XSSDefenseFormat());

		// 设置下载文件大小和存放位置
		me.setUploadedFileSaveDirectory(PathKit.getWebRootPath()
				+ File.separator + "upload");
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/login", LoginController.class);
		me.add("/p", PhotoController.class, "/photo");
		me.add("/g", GalleryController.class, "/gallery");
		me.add("/u", UserController.class, "/user");
		me.add("/mi", MiController.class);
		me.add("/qq", QQController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		// ehcache
		me.add(new EhCachePlugin(PathKit.getWebRootPath() + File.separator
				+ "WEB-INF" + File.separator + "ehcache.xml"));
		// druid
		DruidPlugin druidPlugin = new DruidPlugin(getProperty("mysql.url"),
				getProperty("mysql.username"), getProperty("mysql.password"),
				getProperty("mysql.driverClass"));
		druidPlugin
				.setMaxPoolPreparedStatementPerConnectionSize(getPropertyToInt("druid.maxPoolPreparedStatementPerConnectionSize"));
		me.add(druidPlugin);
		druidPlugin.setInitialSize(getPropertyToInt("druid.initialSize"));
		druidPlugin.setMinIdle(getPropertyToInt("druid.minIdle"));
		druidPlugin.setMaxActive(getPropertyToInt("druid.maxActive"));
		ActiveRecordPlugin mysqlArp = new ActiveRecordPlugin(druidPlugin);
		mysqlArp.setDialect(new MysqlDialect());
		if (getPropertyToBoolean("showSql")) {
			mysqlArp.setShowSql(true);
		}
		mysqlArp.addMapping("t_user", User.class);
		mysqlArp.addMapping("t_gallery", Gallery.class);
		mysqlArp.addMapping("t_photo", Photo.class);
		mysqlArp.addMapping("t_profiles", Profiles.class);
		me.add(mysqlArp);

	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new LoginInterceptor());
		me.add(new SessionInViewInterceptor());

	}

	@Override
	public void configHandler(Handlers me) {

	}

}
